package qrcode

import (
	"github.com/makiuchi-d/gozxing"
	gq "github.com/makiuchi-d/gozxing/qrcode"
	sgq "github.com/skip2/go-qrcode"
	"image"
	"os"
)

func Gen(data string, fn string) error {
	code, err := sgq.New(data, sgq.Medium)
	if err != nil {
		return err
	}
	code.DisableBorder = true
	err = code.WriteFile(256, fn)
	return err
}

func ParseFromFile(file string) (string, error) {
	fi, _ := os.Open(file)
	img, _, _ := image.Decode(fi)
	bmp, _ := gozxing.NewBinaryBitmapFromImage(img)
	qrReader := gq.NewQRCodeReader()
	result, e := qrReader.Decode(bmp, nil)
	if e != nil {
		return "", e
	}
	return result.String(), e
}
